package org.csu.mypetstore.web.servlets;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class CheckoutActionServlet extends HttpServlet {

    public static final String VIEW_SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";
    public static final String VIEW_NEWORDERFORM = "/WEB-INF/jsp/order/NewOrderForm.jsp";
    private Account account;
    private Cart cart;
    private Order order;
    private OrderService orderService;
    private LogService logService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        account = (Account) session.getAttribute("account");

        cart = (Cart) session.getAttribute("cart");
        if (account == null) {
            request.getRequestDispatcher(VIEW_SIGNON).forward(request, response);
        } else {
            order = new Order();
            order.initOrder(account, cart);

            session.setAttribute("order", order);
            account = (Account) session.getAttribute("account");
            LogService logService = new LogService();
            logService = new LogService();
            logService.insertLog("Submit Order", "Submit Order", account.getUsername());

            request.getRequestDispatcher(VIEW_NEWORDERFORM).forward(request, response);
            //}
        }
    }

}
